package com.sky.web.app;

import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController("userDishController")
@RequestMapping("/user/dish")
public class DishController {

    @Autowired
    DishService dishService;
    @Autowired
    RedisTemplate redisTemplate;

    /**
     * 根据分类id查询启售的菜品列表（包含口味）
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    public Result list(@RequestParam Long categoryId){
       //查询redis是否有缓存
        String key = "dish" + categoryId;
        List<DishVO> dishes = (List<DishVO>) redisTemplate.opsForValue().get(key);
        if (dishes!=null&&dishes.size()>0){
            //有缓存，直接返回
            return Result.success(dishes);
        }


        //封装查询条件
        DishDTO dishDTO = DishDTO.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();

        List<DishVO> list = dishService.appList(dishDTO);
        //将数据缓存到redis
        redisTemplate.opsForValue().set(key,list);
        //返回结果
        return Result.success(list);
    }

}
